package com.ruoyi.develop.lvDuxin.service.impl;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.develop.lvDuxindetail.domain.LvDuxindetail;
import com.ruoyi.develop.lvDuxindetail.service.ILvDuxindetailService;
import com.ruoyi.develop.lvDuxindetail.service.impl.LvDuxindetailServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvDuxin.mapper.LvDuxinMapper;
import com.ruoyi.develop.lvDuxin.domain.LvDuxin;
import com.ruoyi.develop.lvDuxin.service.ILvDuxinService;

/**
 * 镀锌单Service业务层处理
 * @author lmm
 * @date 2025-07-18
 */
@Service
public class LvDuxinServiceImpl implements ILvDuxinService {
    @Resource
    private LvDuxinMapper lvDuxinMapper;
    @Resource
    private ILvDuxindetailService lvDuxindetailService;
    /**
     * 查询镀锌单
     * @param id 镀锌单主键
     * @return 镀锌单
     */
    @Override
    public LvDuxin selectById(String id) {
        return lvDuxinMapper.selectById(id);
    }

    /**
     * 查询镀锌单列表
     * @param entity 镀锌单
     * @return 镀锌单
     */
    @Override
    public List<LvDuxin> selectList(LvDuxin entity) {
        return lvDuxinMapper.selectList(entity);
    }

    /**
     * 新增镀锌单
     * @param entity 镀锌单
     * @return 结果
     */
    @Override
    public int insert(LvDuxin entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setCreateTime(DateUtils.getNowDate());
            return lvDuxinMapper.insert(entity);
    }

    /**
     * 修改镀锌单
     * @param entity 镀锌单
     * @return 结果
     */
    @Override
    public int update(LvDuxin entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return lvDuxinMapper.update(entity);
    }

    /**
     * 批量删除镀锌单
     * @param ids 需要删除的镀锌单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvDuxinMapper.deleteByIds(ids);
    }

    /**
     * 删除镀锌单信息
     * @param id 镀锌单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvDuxinMapper.deleteById(id);
    }

    /**
     * 新增入库单 带明细
     *
     * @param entity 入库单 带明细
     * @return 结果
     */
    @Override
    public int addDetail(LvDuxin entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        lvDuxinMapper.insert(entity);

        List<LvDuxindetail> details = entity.getDetailList();
        for (LvDuxindetail detail : details) {
            detail.setParentId(entity.getId());
            // 是否还需要传主表
            lvDuxindetailService.insert(detail);
        }
        return 1;
    }

    /**
     * 修改入库单 带着明细
     *
     * @param entity 入库单
     * @return 结果
     */
    @Override
    public int uodateDetail(LvDuxin entity) {
        // 先删除明细再添加
        lvDuxindetailService.deleteByParentId(entity.getId());

        List<LvDuxindetail> details = entity.getDetailList();
        for (LvDuxindetail detail : details) {
            detail.setParentId(entity.getId());
            // 是否还需要传主表
            lvDuxindetailService.insert(detail);
        }

        return lvDuxinMapper.update(entity);
    }
}
